from django.shortcuts import render
from df_goods.models import Goods
from df_goods.enums import *
from django.core.paginator import Paginator # 导入分页模块
# Create your views here.


def home_list_pate(request):
    # FRUIT: '新鲜水果',
    # SEAFOOD: '海鲜水产',
    # MEAT: '猪牛羊肉',
    # EGGS: '禽类蛋品',
    # VEGETABLES: '新鲜蔬菜',
    # FROZEN: '速冻食品'
    # 获取首页要展示的商品列表
    fruits = Goods.objects.get_goods_list_by_type(goods_type_id=FRUIT, limit=4)
    fruits_new = Goods.objects.get_goods_list_by_type(goods_type_id=FRUIT, limit=3, sort='new')
    seafood = Goods.objects.get_goods_list_by_type(goods_type_id=SEAFOOD, limit=4)
    seafood_new = Goods.objects.get_goods_list_by_type(goods_type_id=SEAFOOD, limit=3, sort='new')
    meat = Goods.objects.get_goods_list_by_type(goods_type_id=MEAT, limit=4)
    meat_new = Goods.objects.get_goods_list_by_type(goods_type_id=MEAT, limit=3, sort='new')
    eggs = Goods.objects.get_goods_list_by_type(goods_type_id=EGGS, limit=4)
    eggs_new = Goods.objects.get_goods_list_by_type(goods_type_id=EGGS, limit=3, sort='new')
    vegetables = Goods.objects.get_goods_list_by_type(goods_type_id=VEGETABLES, limit=4)
    vegetables_new = Goods.objects.get_goods_list_by_type(goods_type_id=VEGETABLES, limit=3, sort='new')
    frozen = Goods.objects.get_goods_list_by_type(goods_type_id=FROZEN, limit=4)
    frozen_new = Goods.objects.get_goods_list_by_type(goods_type_id=FROZEN, limit=3, sort='new')

    # 组织上下文
    context = {
        'fruits': fruits, 'fruits_new': fruits_new,
        'seafood': seafood, 'seafood_new': seafood_new,
        'meat': meat, 'meat_new': meat_new,
        'eggs': eggs, 'eggs_new': eggs_new,
        'vegetables': vegetables, 'vegetables_new': vegetables_new,
        'frozen': frozen, 'frozen_new': frozen_new,
    }
    return render(request, 'df_goods/index.html', context)


# /list/类型id/页码/?sort=排序方式
def goods_list(request, goods_type_id, pindex):
    # 获取排序方式
    sort = request.GET.get('sort', 'default')
    # 获取商品列表
    goods_list = Goods.objects.get_goods_list_by_type(goods_type_id=goods_type_id, sort=sort)

    paginator = Paginator(goods_list, 5) # 进行分页

    pindex = int(pindex)
    goods_list = paginator.page(pindex)     # 返回的是一个Page对象

    # 获取分页后的总页数
    nums_page = paginator.num_pages

    # 小于5页，显示全部
    if nums_page < 5:
        pages = range(1, nums_page+1)
    elif pindex <=3:        # 当前页小于3，显示前5页
        pages = range(1, 6)
    elif nums_page-pindex <=2:  # 最后3页， 显示最后5页
        pages = range(pindex-2, nums_page+1)
    else:       # 中间页，显示当前页和前面两页、后边两页
        pages = range(pindex-2, pindex+3)

    # 获取新品  前两个
    goods_new = Goods.objects.get_goods_list_by_type(goods_type_id=goods_type_id, limit=2, sort='new')

    # 定义上下文
    context = {
        'goods_li': goods_list,
        'type_title': GOODS_TYPE[int(goods_type_id)],
        'type_id': goods_type_id,
        'sort': sort,
        'goods_new': goods_new,
        'pages': pages,      # 页码显示
    }

    return render(request, 'df_goods/list.html', context)


def goods_detail(request, goods_id):
    # 获取商品详细信息
    goods = Goods.objects.get_goods_by_id_with_image(goods_id=goods_id)

    # 获取当前分类的两个最新的商品
    goods_new = Goods.objects.get_goods_list_by_type(goods_type_id=goods.goods_type_id, limit=2, sort='new')

    # 分类名
    type_title = GOODS_TYPE[int(goods.goods_type_id)]

    context = {
        'type_title': type_title,
        'goods': goods,
        'goods_new': goods_new,
    }
    return render(request, 'df_goods/detail.html', context)